package com.tinyshardingjdbc.tinyshardingjdbc.shardingasync;


import cn.hutool.core.util.StrUtil;
import com.tinyshardingjdbc.tinyshardingjdbc.shardingasync.dao.ShardingAsyncDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;


public class TinyShardingAsyncServiceImpl extends TinyShardingAsyncService {


    @Autowired
    ShardingAsyncDao shardingAsyncDao;

    @Override
    public Future<Object> asyncQueryPage(String sql) {
        try {
            List<Map<String, Object>> entityList = shardingAsyncDao.select(sql);
            List<Map<String, Object>> returnList = new ArrayList<>();
            for (Map<String, Object> maps : entityList) {
                Map<String, Object> b = new HashMap<>();
                maps.forEach((k, v) -> b.put(StrUtil.toCamelCase(k),
                        v));
                returnList.add(b);
            }
            return new AsyncResult<>(returnList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(new ArrayList<>());

    }




    @Override
    public Future<HashMap<String, Long>> asyncCount(String sql, String tableName) {
        try {
            Long count = shardingAsyncDao.selectCount(sql);
            HashMap<String, Long> countMap = new HashMap<>();
            countMap.put(tableName, count);
            return new AsyncResult<>(countMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(new HashMap<>());
    }
}
